<template>
  <div class="layui-layout layui-layout-admin">
    <div class="adminnav1">
      <Pagetop></Pagetop>
    </div>
    <div class="adminnav2">
      <Secondnav urlname="homepage"></Secondnav>
    </div>
    <!-- 主体内容 -->
    <div class="layui-body kedou-admin" id="LAY_app_body app">
      <div class="layadmin-tabsbody-item layui-show">
        <div class="pagelist" style="padding:0">
          <div class="layui-tab layui-tab-card">
            <ul class="layui-tab-title">
              <!-- <li class="layui-this" @click='getdate("COMMODITY")'>商品设置</li> -->
              <li class="layui-this" @click='getdate("PVli")'>分享PV统一设置</li>
              <li  @click='getdate("UNIFIED")'>PV总值统一设置</li>
            </ul>
            <div class="layui-tab-content" style="padding:50px 0;font-size:14px;">
              <div class="layui-tab-item layui-show">
                   <div class="layui-form" lay-filter>
                  <div style="padding: 20px; background-color: #F2F2F2;">
                    <div class="layui-row layui-col-space15">
                      <div
                        class="layui-col-md12"
                        v-for="(item,index) in roleTypelist"
                        :value="item.id"
                        :key="index"
                      >
                        <div class="layui-card">
                          <div class="layui-card-body">
                            <div class="layui-form-item">
                              <div class="layui-card-header">{{item.aliasName}}</div>
                            </div>

                            <div class="layui-form-item">
                              <div class="layui-inline">
                                <label class="layui-form-label">经营者</label>
                                <div class="layui-inline">
                                  <div class="layui-input-inline">
                                    <input
                                      type="text"
                                      v-model="dealerIntroducerRofit[index]"
                                      lay-verify="dealerIntroducerRofit"
                                      autocomplete="off"
                                      class="layui-input"
                                    />
                                  </div>
                                  <div class="layui-form-mid">%</div>
                                  <div class="layui-form-mid layui-word-aux">直推分润比例,如10表示10%</div>
                                </div>
                              </div>
                            </div>

                            <div class="layui-form-item">
                              <div class="layui-inline">
                                <label class="layui-form-label">消费者</label>
                                <div class="layui-input-inline">
                                  <select :name="'rofitType'+index" :lay-filter="'rofitnum'+index">
                                    <option value="">请选择</option>
                                    <option
                                      v-for="(its,indexss) in rofitlist"
                                      :selected="its.id==rofitTypeList[index]"
                                      :value="its.id"
                                      :key="indexss"
                                    >{{its.name}}</option>
                                  </select>
                                </div>
                              </div>
                              <div class="layui-inline">
                                <div class="layui-input-inline">
                                  <input
                                    type="text"
                                    required
                                    v-model="consumerRelateRofit[index]"
                                    lay-verify="consumerRelateRofit"
                                    autocomplete="off"
                                    class="layui-input"
                                  />
                                </div>
                                <div class="layui-form-mid">%</div>
                                  <div class="layui-form-mid layui-word-aux">分润比例,如10表示10%</div>
                              </div>
                            </div>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>

                <div class="layui-form-item" style="margin-left:50%;margin-top:20px;">
                  <button class="layui-btn" @click="sure()">保存设置</button>
                </div>

              </div>
              <div class="layui-tab-item" style="font-size:14px">
                <div class="layui-form" lay-filter>
                  <div class="layui-form-item">
              <div class="layui-inline">
                <label class="layui-form-label">PV分润总值</label>
                <div class="layui-input-inline">
                  <input
                    type="text"
                    name
                    v-model="set.amountlimit"
                    placeholder
                    autocomplete="off"
                    class="layui-input"
                  />
                </div>
                <div class="layui-form-mid">%</div>
                <div class="layui-form-mid layui-word-aux">PV分润比例,如10表示10%</div>
              </div>
            </div>
                </div>
                <div class="layui-form-item">
                  <div class="layui-input-block" style="margin-left:10%;margin-top:20px;">
                    <button class="layui-btn" @click="save()">保存设置</button>
                  </div>
                </div>    
               

              </div>

            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import Pagetop from "../common/pagetop.vue";
import Secondnav from "../common/secondnav.vue";
import Tables from "../common/tables.vue";
import Search from "../common/search.vue";
import { log } from 'util';
export default {
  name: "shoppvset",
  components: { Pagetop, Secondnav, Tables, Search },
  data() {
    return {
      pdate: [], //表头
      res: "", //内容
      totalRows: "", //总数量
      curindex: 1, //页数
      btn: [], //按钮
      searchArr: [], //搜索
      isShow: false,
      seapram: "",
      idlist: [],
      rofitType: {
        id: "",
      },
      roleType: {
        id: "",
      },
      dealerIntroducerRofit: [],
      consumerRelateRofit: [],
      rofitTypeList: [],
      rofitlist: [], //分润类型
      roleTypelist: [
        {
          id: "",
          name: "",
          aliasName: "",
        },
      ], //角色类型
      // amountlimit :""
      set:{
        amountlimit:"",
        id:""
      },
      //管理员设置pv分润总值比例
    };
  },
  mounted() {
    var _this = this;
    layui.use("element", function () {
      var element = layui.element;
    });
    this.getdate("PVli");
    this.getrofit();
    this.getroleType();
  },
  methods: {
    getdate: function (type) {
      var _this = this;
      var url = "/profit/aapi/v1.9/platformp-rofit/platformv/byAdmin";
      if (type == "UNIFIED") {
        url = url + "?settingType.in=UNIFIED&dealerId.in=-1&page=0";
        ajaxRequest(url, "GET", "json", "", function (ret, err) {
          if(ret.data!=null){
            _this.set = ret.data;
          }
          
        });
        } else if(type == "PVli"){
            var purl = "/profit/aapi/v1.9/rofitpv";
            ajaxRequest(purl, "GET", "json", "", function (ret, err) {
              if (ret.data.list.length > 0) {
            var arr = ret.data.list;
            for (var i = 0; i < arr.length; i++) {
              _this.rofitTypeList.push(arr[i].rofitType.id) 
              _this.dealerIntroducerRofit.push(arr[i].dealerIntroducerRofit)
              _this.consumerRelateRofit.push(arr[i].consumerRelateRofit)
              _this.idlist.push(arr[i].id);
            }
          }
                
        });
      } 
    },
    sure: function () {
      var _this = this;
      var url = "/profit/aapi/v1.9/rofitpv";
      var arr = _this.roleTypelist;
      if (_this.idlist) {
        for (var i = 0; i < arr.length; i++) {
          arr[i] = {
            rofitType:{
                  id: _this.rofitTypeList[i],
            },
            roleType: _this.roleTypelist[i],
            dealerIntroducerRofit: Number(_this.dealerIntroducerRofit[i]),
            consumerRelateRofit: Number(_this.consumerRelateRofit[i]),
            settingType: "UNIFIED",
            id: _this.idlist[i],
          };
        }
        var dates = arr;
      } else {
        for (var i = 0; i < arr.length; i++) {
          arr[i] = {
            rofitType:{
                  id: _this.rofitTypeList[i],
            },
            roleType: _this.roleTypelist[i],
            dealerIntroducerRofit: Number(_this.dealerIntroducerRofit[i]),
            consumerRelateRofit: Number(_this.consumerRelateRofit[i]),
            settingType: "UNIFIED",
          };
        }
        var dates = arr;
      }
      // alert(JSON.stringify(dates))
      ajaxRequest(url, "POST", "json", dates, function (ret, err) {
        layer.msg("设置成功");
        setTimeout(function () {
          var index = parent.layer.getFrameIndex(window.name);
          parent.layer.close(index);
            parent.location.reload();
        }, 1000);
      });
    },
    save: function () {
      var _this = this;
      var url = '/profit/aapi/v1.9/platformp-rofit';
      if(_this.set.amountlimit){
        var hat = "PUT"
      var dates = {
        amountlimit: _this.set.amountlimit,
        settingType:'UNIFIED',
        id:_this.set.id
      }
      }else{
        var hat = "POST"
        var dates = {
        amountlimit: _this.set.amountlimit,
        settingType:'UNIFIED'
      }
      }
      ajaxRequest(url, hat, 'json', dates, function (ret, err) {
        layer.msg('设置成功');
        setTimeout(function () {
          var index = parent.layer.getFrameIndex(window.name);
          parent.layer.close(index);
            parent.location.reload();
        }, 1000);
      })
    },
    editInfo: function (data) {
      var _this = this;
      layer.open({
        type: 2,
        title: "编辑商品PV总值分润比例",
        content: baseurl + "/shoptype",
        area: _this.screens() < 2 ? ["65%", "25%"] : ["650px", "250px"],
        success: function (layero, index) {
          var iframeWin = window[layero.find("iframe")[0]["name"]];
          iframeWin.getdates(data);
        },
      });
    },
    getrofit: function () {
      var _this = this;
      var url = "/profit/papi/v1.9/rofit-type/";
      ajaxRequest(url, "GET", "json", "", function (ret, err) {
        _this.rofitlist = ret.data;
        // alert(JSON.stringify(_this.rofitlist))
      });
    },
    getroleType: function () {
      var _this = this;
      var url = "/profit/papi/v1.9/role-type";
      ajaxRequest(url, "GET", "json", "", function (ret, err) {
        _this.roleTypelist = ret.data;
        // alert(JSON.stringify(_this.roleTypelist))
      });
    },
    screens: function () {
      return screen();
    },
  },
  updated: function () {
    var _this = this;
    layui.use("form", function () {
      var form = layui.form;
      form.render();
      // form.render(null,'alist');
      // var arr =  _this.rofitTypeList
      // for(var i=0;i<arr.length;i++){ 
      //   var parr  = ['rofitnum0','rofitnum1','rofitnum2']    
      //   form.on("select(parr[1])", function (data) {
      //   arr[i] = data.value;
      //     alert(JSON.stringify(data.value))
      //   });
      // }

      
      form.on("select(rofitnum0)", function (data) {
        _this.rofitTypeList[0] = data.value;
      });
      form.on("select(rofitnum1)", function (data) {
        _this.rofitTypeList[1] = data.value;
      });
      form.on("select(rofitnum2)", function (data) {
        _this.rofitTypeList[2] = data.value;
      });
      form.on("select(rofitnum3)", function (data) {
        _this.rofitTypeList[3] = data.value;
      });
      form.on("select(rofitnum4)", function (data) {
        _this.rofitTypeList[4] = data.value;
      });
      form.on("select(rofitnum5)", function (data) {
        _this.rofitTypeList[5] = data.value;
      });
      form.on("select(rofitnum6)", function (data) {
        _this.rofitTypeList[6] = data.value;
      });
      form.on("select(rofitnum7)", function (data) {
        _this.rofitTypeList[7] = data.value;
      });
      form.on("select(rofitnum8)", function (data) {
        _this.rofitTypeList[8] = data.value;
      });
      form.on("select(rofitnum9)", function (data) {
        _this.rofitTypeList[9] = data.value;
      });
      form.on("select(rofitnum10)", function (data) {
        _this.rofitTypeList[10] = data.value;
      });
      form.on("select(rofitnum11)", function (data) {
        _this.rofitTypeList[11] = data.value;
      });
      form.on("select(rofitnum12)", function (data) {
        _this.rofitTypeList[12] = data.value;
      });
      form.on("select(rofitnum13)", function (data) {
        _this.rofitTypeList[13] = data.value;
      });
      form.on("select(rofitnum14)", function (data) {
        _this.rofitTypeList[14] = data.value;
      });
      form.on("select(rofitnum15)", function (data) {
        _this.rofitTypeList[15] = data.value;
      });
      form.on("select(rofitnum16)", function (data) {
        _this.rofitTypeList[16] = data.value;
      });
      form.on("select(rofitnum17)", function (data) {
        _this.rofitTypeList[17] = data.value;
      });
      form.on("select(rofitnum18)", function (data) {
        _this.rofitTypeList[18] = data.value;
      });
      form.on("select(rofitnum19)", function (data) {
        _this.rofitTypeList[19] = data.value;
      });
      form.on("select(rofitnum20)", function (data) {
        _this.rofitTypeList[20] = data.value;
      });

    });
  },
};
</script>
<style scoped>
/*二级导航*/
</style>